package top.lvkun.pet.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lvkun.basic.utils.AjaxResult;
import top.lvkun.basic.utils.PageList;
import top.lvkun.basic.utils.RedisUtils;
import top.lvkun.org.domain.Employee;
import top.lvkun.pet.domain.Adopt;
import top.lvkun.pet.query.AdoptQuery;
import top.lvkun.pet.service.IAdoptService;
import top.lvkun.user.domain.User;

import javax.servlet.http.HttpServletRequest;

@RestController
public class AdoptController {

    @Autowired
    private IAdoptService adoptService;

    /**
     * 前台用户发布宠物寻主消息
     * @param adopt
     * @return
     */
    @PutMapping("/adopt")
    public AjaxResult publishAdopt(@RequestBody Adopt adopt, HttpServletRequest request){
        try {
            //获取当前登录用户
            User user = RedisUtils.INSTANCE.getUser(request, User.class);
            adopt.setUser_id(user.getId());
            adoptService.publishAdopt(adopt);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发布失败！"+e.getMessage());
        }
    }

    /**
     * 后台管理：分页查询宠物寻主消息
     * @param adoptQuery
     * @return
     */
    @PostMapping("/adopt")
    public PageList<Adopt> pageList(@RequestBody AdoptQuery adoptQuery,HttpServletRequest request){
        Employee admin = RedisUtils.INSTANCE.getUser(request, Employee.class);
        adoptQuery.setShop_id(admin.getShop().getId());
        return adoptService.queryData(adoptQuery);
    }

    /**
     * 后台管理：商家后台处理寻主消息
     * @param id
     * @param handleResult
     * @return
     */
    @GetMapping("/adopt/{id}/{handleResult}")
    public AjaxResult handleAdopt(@PathVariable("id")Long id,@PathVariable("handleResult")Integer handleResult){
        try {
            return adoptService.handleAdopt(id, handleResult);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("处理失败,我们正在殴打程序员" + e.getMessage());
        }
    }
}
